/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function( $, common, ready, Collection, template, Model, dialog,datepicker ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            var _this = this;
            $.ajax({
                url: _global.url.api + 'promotion_oneyuan/'+id,
                type: 'get',
                async: false,
                success: function(res) {
                    if(res.code == 0) {
                        _this.set('info', res.data);
                    }
                }
            });
            this.event();
            this.fetch();
        },
        fetch: function() {
            var _this = this;
            $('#edit-view').html(template('edit-tpl', this.info));
            $('.long_date').on('click', function() {
                WdatePicker({
                    el:this.id,
                    dateFmt:'yyyy-MM-dd HH:mm:ss'
                });
            });
            $.each(this.info.info,function(i,v){
                if(v.goods_sku_id){
                    var attsr_str = v.pro.attr;
                    var attr_str = '';
                    $.each(attsr_str, function(j,u){
                        attr_str += u.name + ':' + u.value + '，';
                    });
                    var attr_str_end = attr_str.substring(0,attr_str.length-1);
                    $('#goods-item').append(template('goods-item-tpl',{goods_id: v.goods_id, goods_name: v.name,goods_pro_name:attr_str_end,goods_pro_id: v.pro.id,pro_price: v.pro.price,val: v.goods_id+''+v.pro.id,promotion_price: v.promotion_price,promotion_total_stock: v.promotion_total_stock,listorder: v.listorder}));
                }else{
                    //console.log({goods_id: v.goods_id, goods_name: v.name,goods_pro_name:'无',goods_pro_id: '0',pro_price: v.price,val: v.goods_id+ '0',promotion_price: v.promotion_price,promotion_total_stock: v.promotion_total_stock,listorder: v.listorder});
                    $('#goods-item').append(template('goods-item-tpl',{goods_id: v.goods_id, goods_name: v.name,goods_pro_name:'无',goods_pro_id: '0',pro_price: v.price,val: v.goods_id+ '0',promotion_price: v.promotion_price,promotion_total_stock: v.promotion_total_stock,listorder: v.listorder}));
                }
                $('.goods-item-del').on('click', function(e) {
                    e.preventDefault();
                    $(this).parent().parent().remove();

                });
            });
            $("#div_goods").show();
            $('#select_goods').on('click', function(e) {
                var site_id=$("#site_id").val();
                if(site_id==""){
                    Alert({msg: '请输入商户site_id', type: 'error'});
                    return false;
                }
                _this.select_goods(site_id,e);
                $("#div_content_goods").show();

            });
            $("#btn-search").on("click",function(e){
                e.preventDefault();
                var site_id = $("#site_id").val();
                _this.select_goods(site_id,e);
            });
            $("#close_goods").on("click",function(e){
                e.preventDefault();
                $("#div_content_goods").hide();
            });
        },
        set: function($k, $v) {
            this[$k] = $v;
        },
        event: function() {
            this.data = {};
            var _this = this;
            $("#btn_save").on("click",function(){
                this.data = {};
                this.data.title = $('#title').val();
                if(this.data.title==''){
                    Alert({msg: '请输入活动标题', type: 'error'});
                    return false;
                }
                var goods_id=new Array();
                $(".goods_id").each(function(){
                    if($(this).val()){
                        goods_id.push($(this).val());
                    }
                });
                this.data.goods_id = goods_id.join(",");
                if( this.data.goods_id==''){
                    Alert({msg: '请选择商品', type: 'error'});
                    return false;
                }
                this.data.type = $('#type').val();
                var goods_pro_id=new Array();
                $(".goods_pro_id").each(function(){
                    if($(this).val()){
                        goods_pro_id.push($(this).val());
                    }
                });
                this.data.goods_pro_id = goods_pro_id.join(",");
                if( this.data.goods_pro_id==''){
                    Alert({msg: '请选择商品规格', type: 'error'});
                    return false;
                }

                var price=new Array();
                var plaf=true;
                $(".promotion_price").each(function(){
                    if($(this).val()==""){
                        plaf=false;
                        return false;
                    }else{
                        plaf=true;
                        price.push($(this).val());
                    }
                });
                if(!plaf){
                    Alert({msg: '请输入抢购售价', type: 'error'});
                    return false;
                }
                var quantity=new Array();
                var plkc=true;
                $(".quantyty").each(function(){
                    if($(this).val()==""){
                        plkc=false;
                        return false;
                    }else{
                        plkc=true;
                        quantity.push($(this).val());
                    }
                });
                if(!plkc){
                    Alert({msg: '请输入抢购数量', type: 'error'});
                    return false;
                }
                this.data.quantyty = quantity.join(',');
                if( this.data.quantyty==''){
                    Alert({msg: '请输入抢购数量', type: 'error'});
                    return false;
                }
                this.data.promotion_price = price.join(",");
                if( this.data.promotion_price==''){
                    Alert({msg: '请输入抢购售价', type: 'error'});
                    return false;
                }
                var listorder=new Array();
                $(".listorder").each(function(){
                    listorder.push($(this).val());
                });
                this.data.listorder = listorder.join(",");
                this.data.activity_id =$("#activity_id").val();
                if( this.data.activity_id==''){
                    Alert({msg:'请输入活动id',type:'error'});
                    return false;
                }
                this.data.dt_start = $('#dt_start').val();
                this.data.id =id;
                new Model({
                    url: _global.url.api + 'promotion_oneyuan',
                    data:this.data
                }).put()
            });
        },
        select_goods:function(site_id,e){
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-items',
                url: _global.url.api + 'promotion_goods_search',
                condition: {site_id: site_id,keyword:$("#keyword").val()},
                model: {
                    event: {
                        '.item-bind:click': 'setoneyuan',
                        '.show-online:click': 'setshow'
                    },
                    setoneyuan:function(e){
                        e.preventDefault();
                        var _this =this;
                        $.ajax({
                            url: _global.url.api + 'oneyuan_goods_pro',
                            type: 'get',
                            async: false,
                            data: {id:this.data.id},
                            success: function(result) {
                                if(result.code == 0) {
                                    goods_pro = result.data;
                                    show_attr(_this.data,goods_pro);
                                }
                            }
                        });
                    }

                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        }



    };
    var show_attr=function(data,goods_pro){
        var flag=true;
        if(goods_pro&&goods_pro!=''){
            var html='';
            html+='<div style="width: 800px;">'
            $.each(goods_pro, function() {
                var attr_arr = this.attr;
                var attr_str = '';
                $.each(attr_arr, function(){
                    attr_str += this.name + ':' + this.value + '，';
                });
                var attr_str_end = attr_str.substring(0,attr_str.length-1);
                html+='<a id="'+this.id+'" date-id="'+this.id+'" val="'+data.id+this.id+'" date-name="'+attr_str_end+'" date-val="'+this.price+'" class="btn btn-default btn_goods_attr">'+attr_str_end+'</a> ';
            });
            html+='</div>';
            flag=true;
        }else{
            var html='<a id="0" date-id="0" val="'+data.id+'0" date-name="没有属性" date-val="'+data.price+'" class="btn btn-default btn_goods_attr">没有属性</a> ';
            flag=false;
        }
        dialog({
            title:'选择商品属性规格',
            content: html,
            ok: function() {
                var goods_id=data.id;
                var goods_name=data.name;
                var goods_pro_id=$(".btn-success").attr("id");
                var goods_pro_name=$(".btn-success").attr("date-name");
                var pro_price=$(".btn-success").attr("date-val");
                if(goods_pro_id==""||goods_pro_id==undefined){
                    Alert({msg: '请选择商品属性', type: 'error'});
                    return false;
                }
                var goods_pro_ids=new Array();
                var val=$(".btn-success").attr("val");
                $(".goods_pro_id").each(function(){
                    if($(this).val()){
                        goods_pro_ids.push($(this).val());
                    }
                });
                var vals=new Array();
                $(".goods_pros_id").each(function(){
                    if($(this).val()){
                        vals.push($(this).val());
                    }
                });
                var goods_pro=vals.join(",");
                if(goods_pro.indexOf(val)!=-1){
                    Alert({'msg':'请不要重复添加同一商品属性','type':'error'});
                    return false;
                }
                $('#goods-item').append(template('goods-item-tpl',{goods_id:goods_id, goods_name:goods_name,goods_pro_name:goods_pro_name,goods_pro_id:goods_pro_id,pro_price:pro_price,val:val}));
                $("#div_goods").show();
                $('.goods-item-del').on('click', function(e) {
                    e.preventDefault();
                    $(this).parent().parent().remove();
                });


            }, cancel: function() {}
        }).show();
        $('.btn_goods_attr').click(function(){
            //属性规格按钮赋样式
            $('.btn_goods_attr').removeClass().addClass('btn btn-default btn_goods_attr');
            $(this).removeClass().addClass('btn btn-success btn_goods_attr');
        });
    }



    new index();

});